package com.hqyj.bigdata_project.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqyj.bigdata_project.entity.CatalogVideoTopTenInfo;
import com.hqyj.bigdata_project.entity.TopTenCatalogInfo;
import com.hqyj.bigdata_project.mapper.CatalogVideoTopTenMapper;
import com.hqyj.bigdata_project.mapper.TopTenCatalogInfoMapper;
import com.hqyj.bigdata_project.service.CatalogVideoTopTenInfoService;
import com.hqyj.bigdata_project.util.HiveUtil;
import com.hqyj.bigdata_project.view.CatalogVideoTopTenView;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

@Service
public class CatalogVideoTopTenInfoServiceImpl extends ServiceImpl<CatalogVideoTopTenMapper, CatalogVideoTopTenInfo> implements CatalogVideoTopTenInfoService {

    @Resource
    private CatalogVideoTopTenMapper catalogVideoTopTenMapper;
    @Transactional(rollbackOn = Exception.class)
    @Override
    public boolean initCatalogVideoTopTenInfo() throws Exception {
        String hql = "select\n" +
                "   categoryId,\n" +
                "   videoId,\n" +
                "   views\n" +
                "from \n" +
                "   (select\n" +
                "   categoryId,\n" +
                "   videoId,\n" +
                "   views,\n" +
                "   rank() over(partition by categoryId order by views desc) rk\n" +
                "from\n" +
                "   gulivideo_category)t1\n" +
                "where rk<=10";
        ResultSet rs = null;
        List<CatalogVideoTopTenInfo> catalogVideoTopTenInfoArrayList = new ArrayList<CatalogVideoTopTenInfo>();
        try {
            rs = HiveUtil.query(hql);
            while (rs.next()) {
                CatalogVideoTopTenInfo catalogVideoTopTenInfo = CatalogVideoTopTenInfo.builder().categoryName(rs.getString("categoryId")).videoId(rs.getString("videoId")).views(rs.getLong("views")).build();
                catalogVideoTopTenInfoArrayList.add(catalogVideoTopTenInfo);
            }
            return super.saveBatch(catalogVideoTopTenInfoArrayList);
        } catch (Exception ex) {
            ex.printStackTrace();
            //手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                    rs = null;
                }
            }catch (Exception ex){
                ex.printStackTrace();
            }
        }
    }


    @Override
    public List<String> queryDistinctCatalogList() {
        return catalogVideoTopTenMapper.queryDistinctCatalogList();
    }

    @Override
    public List<Integer> queryVideoViewsListByCatalogName(String catalogName) {
        return catalogVideoTopTenMapper.queryVideoViewsListByCatalogName(catalogName);
    }

    @Override
    public CatalogVideoTopTenView initCatalogVideoTopTenView() {
        CatalogVideoTopTenView catalogVideoTopTenView = new CatalogVideoTopTenView();
        List<String> catalogList =this.queryDistinctCatalogList();
        if(catalogList!=null && catalogList.size()>0){
            catalogVideoTopTenView.setCatalogList(catalogList);
            List<List<Integer>> catalogVideoViewsList = new ArrayList<List<Integer>>();
            //遍历
            for(String catalogName : catalogList){
                List<Integer> viewsList = new ArrayList<Integer>();
                viewsList = this.queryVideoViewsListByCatalogName(catalogName);
                catalogVideoViewsList.add(viewsList);
            }
            catalogVideoTopTenView.setVideoViewsList(catalogVideoViewsList);
        }
        return catalogVideoTopTenView;

    }
}
